package com.meituan.meituanservice.controller;

import com.meituan.meituanservice.model.Address;
import com.meituan.meituanservice.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sound.midi.Soundbank;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AddressController {
    @Autowired
    AddressService addressService;

    @GetMapping("getAddress")
    public Map getAddress(String userId){
        Map map = new HashMap();
        List<Address> list =  addressService.GetAddressByUserId(userId);
        if(list.size()==0){
            map.put("err_code",1);
        }else{
            map.put("err_code",0);
            map.put("address",list);
        }
        return map;
    }

    @GetMapping("deleteAddress")
    public Map deleteAddress(String addressId){
        Map map = new HashMap();
        Integer result = addressService.deleteAddress(addressId);
        if(result==1){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @PostMapping("addAddress")
    public Map addAddress(@RequestBody Address address){
        Map map = new HashMap();
        System.out.println(address);
        if(addressService.add(address)==1){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

    @PostMapping("editAddress")
    public Map editAddress(@RequestBody Address address){
        Map map = new HashMap();
        if(addressService.update(address)==1){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }
        return map;
    }

}
